package com.stefanie.nettydemo.test1;

import com.stefanie.nettydemo.utils.ByteBufferUtil;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
 * @author:stefanie-zy
 * @time:2023/12/4-16:52
 * @description: byteBuffer和字符串的转换
 */
public class ByteBufferStringTest {

    public static void main(String[] args) {
        String name = "stefanie-zy-test";
        // 字符串转ByteBuffer。写模式
        ByteBuffer buffer = ByteBuffer.allocate(16);
        buffer.put(name.getBytes());
        ByteBufferUtil.debugAll(buffer);

        // 使用编码。直接切换至读模式
        ByteBuffer buffer1 = StandardCharsets.UTF_8.encode(name);
        ByteBufferUtil.debugAll(buffer1);

        // Wrap。直接切换至读模式
        ByteBuffer buffer2 = ByteBuffer.wrap(name.getBytes());
        ByteBufferUtil.debugAll(buffer2);
    }
}
